Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report nullable ctor warnings for struct primary constructors #74844

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

RikkiGibson
Copy link
Contributor

Closes #74726

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 22, 2024
Comment on lines -1738 to -1743
if (method is SynthesizedPrimaryConstructor primaryCtor && method.ContainingType.IsStructType())
{
body = BoundBlock.SynthesizedNoLocals(primaryCtor.GetSyntax());
nullableInitialState = getInitializerState(body);
}
else if (method is SourceMemberMethodSymbol sourceMethod)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path was causing us to miss nullable analyzing the struct primary constructor.

@@ -639,8 +639,7 @@ void enforceMemberNotNull(SyntaxNode? syntaxOpt, LocalState state)
Debug.Assert(thisParameter is object);
thisSlot = GetOrCreateSlot(thisParameter);
}
// https://github.com/dotnet/roslyn/issues/46718: give diagnostics on return points, not constructor signature
var exitLocation = method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation();
var exitLocation = method is SynthesizedPrimaryConstructor || method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a type has a primary constructor, all other instance constructors will be required to chain to it (directly or indirectly), so it will be the only constructor which will get warnings for uninitialized non-nullable instance fields. That means the field/property declaration is really the best place to report diagnostics in such cases.

@RikkiGibson RikkiGibson marked this pull request as ready for review August 22, 2024 19:12
@RikkiGibson RikkiGibson requested a review from a team as a code owner August 22, 2024 19:12
@RikkiGibson
Copy link
Contributor Author

@dotnet/roslyn-compiler for review

@@ -1735,12 +1735,7 @@ private static void GetStateMachineSlotDebugInfo(

initializersBody ??= GetSynthesizedEmptyBody(method);

if (method is SynthesizedPrimaryConstructor primaryCtor && method.ContainingType.IsStructType())
{
body = BoundBlock.SynthesizedNoLocals(primaryCtor.GetSyntax());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What replicates this logic now? What body is created for the primary constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same logic which was used for class primary constructors is now also used for struct primary constructors. You can see that where the assertions were adjusted.

I reviewed the code around this area and didn't spot anything which would be problematic for structs, but, it's quite possible I missed something.

@RikkiGibson RikkiGibson merged commit 1562f12 into dotnet:main Aug 27, 2024
24 checks passed
@RikkiGibson RikkiGibson deleted the struct-primary-ctor-nullable branch August 27, 2024 18:53
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Aug 27, 2024
@akhera99 akhera99 modified the milestones: Next, 17.12 P3 Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nullable field warnings are missing when using struct primary constructors
4 participants